<?php
class SitemapController extends DefaultBaseController
{
	
	public function indexAction() {
		$this->view->articles = Obj_Articles::getNoPagingArticles(true);
		
		$active_countries = Obj_Country::getCountries(true);
		$active_regions = array();
		foreach($active_countries as $v){
			$active_regions[$v['ID']] = Obj_Region::getRegions($v['ID'], true);
		}
		
		$this->view->countries = $active_countries;
		$this->view->regions = $active_regions;
		self::setMetaInfo(_('Карта сайта'), _('Недвижимость в Турции. Квартиры в Турции, виллы в Турции, квартиры в Алании, купить квартиру в Турции, недвижимость в Италии, недвижимость в России, зарубежная недвижимость Турция, купить недвижимость, аренда недвижимости, агентство зарубежной недвижимости'), _('Весь комплекс услуг по покупке, продаже, аренде недвижимости в Турции, Италии, России предоставляет агентство недвижимости aurora investment. Скидки, специальные предложения на покупку квартир, вилл, апартаментов, и другой недвижимости.'));
    }
    
    public function generateAction() {
		$xml = new DOMDocument('1.0', 'utf-8');
		$xml->formatOutput = true;
		$urlset = $xml->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset');
		//$urlset->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
		//$urlset->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
		
		
		
		$xml->appendChild($urlset);
		
		
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '1.00');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/contacts');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '1.00');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		
		/*===== NEWS ================*/
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/news');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '0.80');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$news = Obj_News::getSitemapNews();
		foreach ($news as $v) {
			$url = $xml->createElement('url');
			$loc = $xml->createElement('loc', HTTP_HOST.'/news/'.$v['alias']);
			$changefreq = $xml->createElement('changefreq', 'daily');
			$priority = $xml->createElement('priority', '0.80');
			$lastmod = $xml->createElement('lastmod', date('Y-m-d', strtotime($v['date'])));
			$url->appendChild($loc);
			$url->appendChild($changefreq);
			$url->appendChild($priority);
			$url->appendChild($lastmod);
			$urlset->appendChild($url);
		}
		/*===== NEWS ================*/
		
		/*===== ARTICLES ================*/
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/articles');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '1.00');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$articles = Obj_Articles::getSitemapArticles();
		foreach ($articles as $v) {
			$url = $xml->createElement('url');
			$loc = $xml->createElement('loc', HTTP_HOST.'/articles/'.$v['alias']);
			$changefreq = $xml->createElement('changefreq', 'daily');
			$priority = $xml->createElement('priority', '0.80');
			$lastmod = $xml->createElement('lastmod', date('Y-m-d', strtotime($v['date'])));
			$url->appendChild($loc);
			$url->appendChild($changefreq);
			$url->appendChild($priority);
			$url->appendChild($lastmod);
			$urlset->appendChild($url);
		}
		/*===== ARTICLES ================*/
		
		/*===== SPECIAL OFFERS ================*/
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/special-offers');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '0.80');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$offers = Obj_SpecialOffer::getSitemapOffers();
		foreach ($offers as $v) {
			$url = $xml->createElement('url');
			$loc = $xml->createElement('loc', HTTP_HOST.'/special-offers/index/id/'.$v['ID']);
			$changefreq = $xml->createElement('changefreq', 'daily');
			$priority = $xml->createElement('priority', '0.80');
			$lastmod = $xml->createElement('lastmod', date('Y-m-d', strtotime($v['date'])));
			$url->appendChild($loc);
			$url->appendChild($changefreq);
			$url->appendChild($priority);
			$url->appendChild($lastmod);
			$urlset->appendChild($url);
		}
		/*===== SPECIAL OFFERS ================*/
		
		/*===== SERVICE ACTIONS ================*/
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/service-action');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '0.80');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/service-action/individual-selection-of-real-estate');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '0.80');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/service-action/organization-of-study-tour');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '0.80');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/service-action/with-legal');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '0.80');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/service-action/getting-loan-or-mortgage');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '0.80');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/service-action/after-sales-service');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '0.80');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		$url = $xml->createElement('url');
		$loc = $xml->createElement('loc', HTTP_HOST.'/service-action/property-management');
		$changefreq = $xml->createElement('changefreq', 'daily');
		$priority = $xml->createElement('priority', '0.80');
		$url->appendChild($loc);
		$url->appendChild($changefreq);
		$url->appendChild($priority);
		$urlset->appendChild($url);
		
		/*===== SERVICE ACTIONS ================*/
		
		
		/*===== OBJECTS ==============*/
		$arCountries = Obj_Country::getCountries(true);
		foreach ($arCountries as $country) {
			$url = $xml->createElement('url');
			$loc = $xml->createElement('loc', HTTP_HOST.'/'.$country['alias']);
			$changefreq = $xml->createElement('changefreq', 'daily');
			$priority = $xml->createElement('priority', '1.00');
			$lastmod = $xml->createElement('lastmod', date('Y-m-d'));
			$url->appendChild($loc);
			$url->appendChild($changefreq);
			$url->appendChild($priority);
			$url->appendChild($lastmod);
			$urlset->appendChild($url);
			
			/*===== COUNTRY PAGES ==============*/
			$arPages = Obj_LocationContentPage::getLocationContentPages($country['ID'], CONTENT_TYPE_COUNTRY, true);
			foreach ($arPages as $page) {
				$url = $xml->createElement('url');
				$loc = $xml->createElement('loc', HTTP_HOST.'/'.$country['alias'].'/info/'.$page['alias']);
				$changefreq = $xml->createElement('changefreq', 'daily');
				$priority = $xml->createElement('priority', '1.00');
				$lastmod = $xml->createElement('lastmod', date('Y-m-d'));
				$url->appendChild($loc);
				$url->appendChild($changefreq);
				$url->appendChild($priority);
				$url->appendChild($lastmod);
				$urlset->appendChild($url);
			}
			/*===== COUNTRY PAGES ==============*/
			
			/*===== REGIONS ==============*/
			$arRegions = Obj_Region::getRegions($country['ID'], true);
			foreach ($arRegions as $region) {
				$url = $xml->createElement('url');
				$loc = $xml->createElement('loc', HTTP_HOST.'/'.$country['alias'].'/'.$region['alias']);
				$changefreq = $xml->createElement('changefreq', 'daily');
				$priority = $xml->createElement('priority', '1.00');
				$lastmod = $xml->createElement('lastmod', date('Y-m-d'));
				$url->appendChild($loc);
				$url->appendChild($changefreq);
				$url->appendChild($priority);
				$url->appendChild($lastmod);
				$urlset->appendChild($url);
				
				/*===== CITIES ==============*/
				$arCities = Obj_City::getCities($region['ID'], true);
				foreach ($arCities as $city) {
					$url = $xml->createElement('url');
					$loc = $xml->createElement('loc', HTTP_HOST.'/'.$country['alias'].'/'.$region['alias'].'/'.$city['alias']);
					$changefreq = $xml->createElement('changefreq', 'daily');
					$priority = $xml->createElement('priority', '1.00');
					$lastmod = $xml->createElement('lastmod', date('Y-m-d'));
					$url->appendChild($loc);
					$url->appendChild($changefreq);
					$url->appendChild($priority);
					$url->appendChild($lastmod);
					$urlset->appendChild($url);
					
					/*===== LOCATIONS ==============*/
					$arLocations = Obj_Location::getLocations($city['ID'], true);
					foreach ($arLocations as $location) {
						$url = $xml->createElement('url');
						$loc = $xml->createElement('loc', HTTP_HOST.'/'.$country['alias'].'/'.$region['alias'].'/'.$city['alias'].'/'.$location['alias']);
						$changefreq = $xml->createElement('changefreq', 'daily');
						$priority = $xml->createElement('priority', '1.00');
						$lastmod = $xml->createElement('lastmod', date('Y-m-d'));
						$url->appendChild($loc);
						$url->appendChild($changefreq);
						$url->appendChild($priority);
						$url->appendChild($lastmod);
						$urlset->appendChild($url);
						
						
						/*===== LOCATIONS ==============*/
						$arObjects = Obj_Object::getSitemapObjects($location['ID']);
						foreach ($arObjects as $object) {
							$url = $xml->createElement('url');
							$loc = $xml->createElement('loc', HTTP_HOST.'/'.$country['alias'].'/'.$region['alias'].'/'.$city['alias'].'/'.$location['alias'].'/'.$object['alias'].'/id'.$object['ID']);
							$changefreq = $xml->createElement('changefreq', 'daily');
							$priority = $xml->createElement('priority', '1.00');
							$lastmod = $xml->createElement('lastmod', date('Y-m-d'));
							$url->appendChild($loc);
							$url->appendChild($changefreq);
							$url->appendChild($priority);
							$url->appendChild($lastmod);
							$urlset->appendChild($url);
						}
						/*===== LOCATIONS ==============*/
						
						
					}
					/*===== LOCATIONS ==============*/
					
					
				}
				/*===== CITIES ==============*/
				
			}
			/*===== REGIONS ==============*/
		}
		
		
		/*===== OBJECTS ==============*/
		
		$s = $xml->saveXML();
		file_put_contents(APPLICATION_PATH.'/../public/sitemap.xml', $s);
		die();    	
    }

}